<?php

namespace App\Admin\Controllers\Aliyun;

use App\Admin\Actions\GoBackButton;
use App\Enums\BucketType;
use App\Models\Aliyun;
use App\Models\Cdn;
use App\Models\ObjectStorageService;
use Dcat\Admin\Exception\InvalidArgumentException;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;
use Symfony\Component\HttpFoundation\Response;

class CdnController extends Controller
{
    /**
     * @throws InvalidArgumentException
     */
    public function index(Content $content, Aliyun $aliyun): Content
    {
        return $content->header($aliyun->name)
            ->description('CDN 管理')
            ->body($this->grid($aliyun));
    }

    /**
     * @throws InvalidArgumentException
     */
    public function grid(Aliyun $aliyun)
    {
        return Grid::make(Cdn::initAliyun($aliyun), function (Grid $grid) use ($aliyun) {
            $grid->disableEditButton();

            $grid->tools([
                new GoBackButton,
            ]);

            $grid->column('DomainName', '域名');
            $grid->column('CdnType')
                ->using([
                    'web' => '图片小文件',
                    'download' => '大文件下载',
                    'video' => '视音频点播',
                ]);
            $grid->column('Cname')
                ->copyable();
            $grid->column('DomainStatus')
                ->using([
                    'online' => '正常运行',
                    'offline' => '停用',
                    'configuring' => '配置中',
                    'configure_failed' => '配置失败',
                    'checking' => '正在审核',
                    'check_failed' => '审核失败',
                    'stopping' => '停用中',
                    'deleting' => '删除中',
                ])
                ->dot([
                    'online' => 'success',
                    'offline' => 'danger',
                    'configuring' => 'default',
                    'configure_failed' => 'danger',
                ]);
            $grid->column('SslProtocol')
                ->using([
                    'on' => '已开启',
                    'off' => '未开启',
                ])
                ->label([
                    'on' => 'success',
                    'off' => 'default',
                ]);
            $grid->column('GmtCreated')
                ->dateFormat();
        });
    }

    /**
     * @throws InvalidArgumentException
     */
    public function create(Content $content, Aliyun $aliyun): Content
    {
        return $content->header($aliyun->name)
            ->description('创建CDN')
            ->body($this->form($aliyun));
    }

    /**
     * @throws InvalidArgumentException
     */
    public function form(Aliyun $aliyun): Form
    {
        return Form::make(Cdn::initAliyun($aliyun), function (Form $form) use ($aliyun) {
            $form->row(function (Form\Row $form) use ($aliyun) {
                $form->width(4)
                    ->text('host', '主机记录')
                    ->required();
                $form->width(8)
                    ->select('domain', '加速域名')
                    ->options(collect($aliyun->getDomains())->pluck('text', 'id'))
                    ->required();
                $form->select('content', 'OSS地址')
                    ->required()
                    ->options(
                        ObjectStorageService::ofAdmin()
                            ->where('type', BucketType::STORAGE)
                            ->pluck('bucket', 'bucket')
                    );
            });
        });
    }

    /**
     * @throws InvalidArgumentException
     */
    public function edit(Content $content, Aliyun $aliyun, string $domain): Content
    {
        return $content->header($aliyun->name)
            ->description('编辑CDN')
            ->body($this->form($aliyun)->edit($domain));
    }

    /**
     * @throws InvalidArgumentException
     * @throws Exception
     */
    public function update(Aliyun $aliyun, string $domain): Response
    {
        return $this->form($aliyun)->update($domain);
    }

    /**
     * @throws InvalidArgumentException
     * @throws Exception
     */
    public function store(Aliyun $aliyun): JsonResponse
    {
        return $this->form($aliyun)->store();
    }

    /**
     * @throws InvalidArgumentException
     * @throws Exception
     */
    public function destroy(Aliyun $aliyun, string $domain): JsonResponse
    {
        return $this->form($aliyun)->destroy($domain);
    }
}
